package com.jt.rabbitmq.work;

import com.jt.utils.ConnectionUtil;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class ConsumerThread extends Thread {

    public ConsumerThread(String name) {
        super(name);
    }

    public ConsumerThread() {

    }
    private static final String QUEUE_NAME = "workThread——test";

    private  String name;
    @Override
    public void run() {

        ConnectionUtil ConnectionUtil = new ConnectionUtil();
        try {
            Thread.sleep(200);
            Connection connection = ConnectionUtil.getConnection();
            Channel channel = connection.createChannel();
            channel.queueDeclare(QUEUE_NAME, true, false, false, null);
            Consumer consumer=new DefaultConsumer(channel){
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    System.out.println(Thread.currentThread().getName()+">>>>>>>>>>"+new String(body));
                }
            };
            channel.basicConsume(QUEUE_NAME,true,consumer);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }


    public static void main(String[] args) {
        ConsumerThread consumerThread1=new ConsumerThread("线程Consumer1");
        ConsumerThread consumerThread2=new ConsumerThread("线程Consumer2");
        Thread thread1= new Thread(consumerThread1);
        Thread thread2 = new Thread(consumerThread2);
        thread1.start();
        thread2.start();
    }
}
